package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.bean.BaseBean;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.query.ProjectListActivity;
import com.cmcc.linlaker.ui.upload.adapter.UploadQuestionAdapter;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UploadHistoryActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_history);

        BarUtils.initStatusBar(this);

        initView();
    }

    private UploadQuestionAdapter adapter;
    private RecyclerView recycler_upload;
    private List<UploadQuestionBean.UploadQuestionData> uploadList = new ArrayList<>();

    private MaterialRefreshLayout refreshLayout;

    public void initView() {
        recycler_upload = findViewById(R.id.recycler_upload);
        setRecyclerView(recycler_upload, uploadList);

        refreshLayout = findViewById(R.id.refresh);
        refreshLayout.setLoadMore(true);
        refreshLayout.setWaveShow(true);
        refreshLayout.setWaveColor(Color.parseColor("#3eaefc"));

        refreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        reset(false);
                    }
                }, 500);
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                if (isHasLoaded) {
                    ToastUtils.showText(UploadHistoryActivity.this, "数据已全部加载");
                    refreshLayout.finishRefreshLoadMore();
                } else {
                    getProblemList(false);
                }
            }
        });

        getProblemList(true);
    }

    private int pageSize = 20;
    private int pageNum = 1;
    private boolean isHasLoaded = false;

    private void reset(boolean isShowDialog) {
        uploadList.clear();
        pageNum = 1;
        isHasLoaded = false;
        getProblemList(isShowDialog);
    }

    private void setRecyclerView(RecyclerView recycler_view, final List<UploadQuestionBean.UploadQuestionData> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));

        recycler_view.setNestedScrollingEnabled(false);

        adapter = new UploadQuestionAdapter(this, itemList);
        adapter.setOnItemClickListener(new UploadQuestionAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(UploadHistoryActivity.this, UploadDetailActivity.class);
                intent.putExtra("problemBean", itemList.get(position));
                startActivity(intent);
            }
        });
        recycler_view.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public void back(View view) {
        finish();
    }

    /**
     * 获取列表
     */
    private void getProblemList(boolean isShowDialog) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        Map<String, String> params = new HashMap<>();
        params.put("pageSize", pageSize + "");
        params.put("pageNum", pageNum + "");
        params.put("isAsc", "desc");
        helper.sendPOSTRequest(ApiDefine.PROBLEM_LIST, UploadQuestionBean.class, params, isShowDialog, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    UploadQuestionBean bean = (UploadQuestionBean) data;
                    if (bean.code.equals("0")) {
                        List<UploadQuestionBean.UploadQuestionData> rows = bean.rows;

                        if (rows.size() == pageSize) {
                            uploadList.addAll(bean.rows);
                            adapter.notifyDataSetChanged();
                            pageNum++;
                        } else {
                            uploadList.addAll(bean.rows);
                            adapter.notifyDataSetChanged();
                            isHasLoaded = true;
                        }
                        refreshLayout.finishRefresh();
                        refreshLayout.finishRefreshLoadMore();

                        if (uploadList.size() > 0) {
                            recycler_upload.setVisibility(View.VISIBLE);
                            findViewById(R.id.layout_no_data).setVisibility(View.GONE);
                        } else {
                            recycler_upload.setVisibility(View.GONE);
                            findViewById(R.id.layout_no_data).setVisibility(View.VISIBLE);
                        }
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(UploadHistoryActivity.this);
                    }
                }
            }
        });
    }

    /**
     * 新建上报
     */
    public void newUpload(View view) {
        Intent intent = new Intent(UploadHistoryActivity.this, NewUploadQuestionActivity.class);
        startActivity(intent);
    }

}
